import {
    checkOutOrder,
    checkOutSubmit,
    orderPay,
    getGoodsDetail,
    addressDetail,
    addressDefaultId,
    addressList
} from "../../service/index.js";

Page({
    data: {
        cartIds: [],
        totalPrice: 0,
        cartTotal: 0,
        remark: '',
        address: '',
        data: {},
        cartList: []
    },
    onLoad: function (options) {
        this.getAddress()
        if (options.data) {
          this.getGoodsDetail(options.goodsId)
            this.setData({
                data: JSON.parse(options.data),
                totalPrice: options.totalPrice / 100,
                cartTotal: options.cartTotal,
            })
        } else {
            this.setData({
                cartIds: options.cartIds,
                totalPrice: options.totalPrice / 100,
                cartTotal: options.cartTotal,
                cartList: JSON.parse(options.cartList)
            })
        }
    },
    go2Address(event) {
        wx.navigateTo({
            url: '/pages/addressList/index'
        })
    },
    getAddress(event) {
        addressDefaultId({}, (res) => {
            addressDetail({
                addressId: res.data.defaultId
            }, (res2) => {
                this.setData({
                    address: res2.data.detail
                })
            })
        })
    },
    getGoodsDetail: function (goodsId) {
        getGoodsDetail(goodsId,(res)=>{
            this.setData({
                cartList: [{goods: res.data.detail}]
            })
        })
    },
    getCheckOutOrder: function () {
        checkOutOrder(this.data.data == null ? {
            mode: 'cart',
            cartIds: this.data.cartIds,
            delivery: 10,
            couponId: 0,
            isUsePoints: 0
        } : this.data.data, (res) => {
        })
    },
    submitOrder: function () {
        checkOutSubmit(this.data.data == null ? {
            mode: 'cart',
            cartIds: this.data.cartIds,
            delivery: 10,
            couponId: 0,
            payType: 10,
            remark: this.data.remark,
            isUsePoints: 0
        }:this.data.data, (res) => {
            this.orderPay(res.data)
            wx.navigateTo({
                url: '/pages/orders/index'
            })
        })
    },
    orderPay: function (param) {
        orderPay({
            orderId: param.orderId,
            payType: param.payType,
        }, (res) => {
        })
    }
});
